import Vue from 'vue'
import axios from 'axios'

let Util = {}
Util.install = function (Vue, options) {



  Vue.formatDateTime = Vue.prototype.$formatDateTime = function (time, str) {
    let theDate
    if (time) {
      theDate = new Date(time)
    } else {
      theDate = new Date()
    }
    var _hour = theDate.getHours()
    var _minute = theDate.getMinutes()
    var _second = theDate.getSeconds()
    var _year = theDate.getFullYear()
    var _month = theDate.getMonth()
    var _date = theDate.getDate()
    if(_hour<10){_hour="0"+_hour}
    if(_minute<10){_minute="0"+_minute}
    if(_second<10){_second="0"+_second}
    _month = _month + 1
    if(_month < 10){_month = "0" + _month}
    if(_date<10){_date="0"+_date  }
    if (str) {
      return  _year + str + _month + str + _date + " " + _hour + ":" + _minute + ":" + _second
    }
    return  _year + "-" + _month + "-" + _date + " " + _hour + ":" + _minute + ":" + _second
  }

  Vue.formatDateTime = Vue.prototype.$formatDateTimeChinese = function (time) {
    let theDate
    if (time) {
      theDate = new Date(time)
    } else {
      theDate = new Date()
    }
    var _hour = theDate.getHours()
    var _minute = theDate.getMinutes()
    var _second = theDate.getSeconds()
    var _year = theDate.getFullYear()
    var _month = theDate.getMonth()
    var _date = theDate.getDate()
    if(_hour<10){_hour="0"+_hour}
    if(_minute<10){_minute="0"+_minute}
    if(_second<10){_second="0"+_second}
    _month = _month + 1
    if(_month < 10){_month = "0" + _month}
    if(_date<10){_date="0"+_date  }

    return  _year + "年" + _month + "月" + _date + "日  " + _hour + ":" + _minute + ":" + _second
  }

  Vue.formatYMD = Vue.prototype.$formatYMD = function (time, str) {
    let theDate = new Date(time)
    var _hour = theDate.getHours()
    var _minute = theDate.getMinutes()
    var _second = theDate.getSeconds()
    var _year = theDate.getFullYear()
    var _month = theDate.getMonth()
    var _date = theDate.getDate()
    if(_hour<10){_hour="0"+_hour}
    if(_minute<10){_minute="0"+_minute}
    if(_second<10){_second="0"+_second}
    _month = _month + 1
    if(_month < 10){_month = "0" + _month}
    if(_date<10){_date="0"+_date  }
    if (str) {
      return  _year + str + _month + str + _date
    }
    return  _year + "-" + _month + "-" + _date
  }


  // 对字符串进行加密
  Vue.compileStr = Vue.prototype.$compileStr = function (code) {
    let c = String.fromCharCode(code.charCodeAt(0) + code.length)
    for (let i = 1; i < code.length; i++) {
      c += String.fromCharCode(code.charCodeAt(i) + code.charCodeAt(i - 1))
    }
    return escape(c)
  }
  // 字符串进行解密
  Vue.uncompileStr = Vue.prototype.$uncompileStr = function (code) {
    code = unescape(code)
    let c = String.fromCharCode(code.charCodeAt(0) - code.length)
    for (var i = 1; i < code.length; i++) {
      c += String.fromCharCode(code.charCodeAt(i) - c.charCodeAt(i - 1))
    }
    return c
  }
  // 通过地址栏的参数获取参数内容
  Vue.getUrlParames = Vue.prototype.$getUrlParames = function (param) {
    var query = window.location.search
    var iLen = param.length
    var iStart = query.indexOf(param)
    if (iStart === -1) {
      return ''
    }
    iStart += iLen + 1
    var iEnd = query.indexOf('&', iStart)
    if (iEnd === -1) {
      return query.substring(iStart)
    }
    return query.substring(iStart, iEnd)
  }
  // 从地址栏获取加密的参数
  Vue.getUrlCompileParames = Vue.prototype.$getUrlCompileParames = function (param) {
    var query = Vue.uncompileStr(window.location.search.slice(1, window.location.search.length))
    var iLen = param.length
    var iStart = query.indexOf(param)
    if (iStart === -1) {
      return ''
    }
    iStart += iLen + 1
    var iEnd = query.indexOf('&', iStart)
    if (iEnd === -1) {
      return query.substring(iStart)
    }
    return query.substring(iStart, iEnd)
  }


  // 拼接condition
  Vue.getCondition = Vue.prototype.$getCondition = function (condition) {
    let con = '1=1'
    Object.keys(condition).forEach(key => {
      if (condition[key] && condition[key].length > 0) {
        con += ` and ${key} like '%${condition[key]}%'`
      }
    })
    return con
  }

  Vue.lsput = Vue.prototype.$lsput = function (key, value) {
    if (typeof value === 'object') {
      value = JSON.stringify(value)
    }
    localStorage.setItem(key, value)
  }

  Vue.lsget = Vue.prototype.$lsget = function (key) {
    let val = localStorage.getItem(key)
    try {
      return JSON.parse(val)
    } catch (e) {
      // console.log(e)
      return val
    }
  }

  Vue.lsget = Vue.prototype.$lsdelete = function (key) {
    localStorage.removeItem(key)
  }

  Vue.shuffle = Vue.prototype.$shuffle = function (arr) {
    let i
    let j
    let temp
    for (i = arr.length - 1; i > 0; i--) {
      j = Math.floor(Math.random() * (i + 1))
      temp = arr[i]
      arr[i] = arr[j]
      arr[j] = temp
    }
    return arr
  }
}

export default Util
